﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using System.Threading.Tasks;
using SinaWeiboClientV1;
using System.Threading;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using GalaSoft.MvvmLight.Command;

namespace SinaWeiboApp_WP7.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        public const string AppKey = "967389259";
        public const string AppSecret = "001379b11292669a5399131cfc5b806d";

        public LoginViewModel()
        {
            _id = "jdcbtest@sina.com";
            _password = "jdcbtest";
            
            _title = "登录";
            _loginEnabled = true;

            LoginCommand = new RelayCommand(async () => {
                await Login();
            });

            TestCommand = new RelayCommand(() =>
            {
                MessageBox.Show("Test");
            });
        }

        private string _id;
        public string Id
        { 
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                RaisePropertyChanged("Id");
            }
        }

        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }

        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        private bool _loginEnabled;
        public bool LoginEnabled
        {
            get
            {
                return _loginEnabled;
            }
            set
            {
                _loginEnabled = value;
                RaisePropertyChanged("LoginEnabled");
            }
        }

        public async Task Login()
        {
            try
            {
                LoginEnabled = false;
                var cancel = new CancellationToken();
                var token = await TokenHelper.CreateOAuthTokenAsync(AppKey, AppSecret,
                                                Id, Password, cancel);

                var client = new WeiboClient();
                client.CurrentAuthToken = token;
                var userShowRequest = new UsersShowRequest { Id = client.CurrentAuthToken.ContextData["UserID"].ToString() };
                var userShow = await client.ExecuteUsersShowAsync(userShowRequest);

                App.Token = token;
                App.UserInfo = userShow.Value;

                PhoneApplicationFrame frame = App.Current.RootVisual as PhoneApplicationFrame;
                frame.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException == null ? ex.Message : ex.InnerException.Message);
            }
            finally
            {
                LoginEnabled = true;
            }
        }

        public RelayCommand LoginCommand
        {
            get;
            private set;
        }

        public RelayCommand TestCommand
        {
            get;
            private set;
        }
    }
}
